EHCI Computer Science 20 (VBA) (click here for Scratch)

Enter e-mail for this class
Sample Survey
Tutorial Site here.
Click here to provide E-mail address for progress reports.
Digitized 2012 May 3rd Register NOW:1
http://digitized.tumblr.com/
If you are having trouble seeing your userform in any of the files posted here, change the security settings!

Don't forget to save the Userform under the ThisDocument (NOT the Default Template!)
VBA.PNG

The class textbook is available at this link (school username/password needed). Click here for troubleshooting login issues.
We will learn how to write computer programs in this introductory Computer Science course. We will be using the language Visual Basic For Applications (VBA). For students interested in compiling programs, click here for Visual Basic 2010 Express. If you would like the full Visual Studio 2010, see your teacher (MS Dreamspark)

January 30: Number Systems

Notes:

Worksheet:

January 31: Numbers systems continued
Here is the Excel spreadsheet we used in class to help work out decimal-binary and binary-decimal.
For the Google Docs spreadsheet, click here.
The calculator has been updated for octal and hex.

February 1st: Chapter 3 (introducing VBA environment)
click here for a video about how to program with VBA (part 1)
click here for part 2 of the video.
February 2nd: Chapter 4 (do the even questions)
The template for #4-2 .. 4-6 is here (Word 2003).

Here are the Word 2010 Macro Enabled files:




February 3rd: Chapter 4 (continued)

February 6th: Order of operations and 3 different division operators in Visual Basic (4-8 Help.docm)
Note: when working with radio buttons, draw a frame first, then put the option buttons inside the frame!

February 7th: Frames and Radio Buttons, putting more than one line of text in a single label

How to add a picture to your user form i VBA:


February 8th: Finish up 4-8

February 10th: Start Chap 5 or Scratch

Chinese zodiac VBA project (zipped)
The first 2 are done for you. Finish it.

February 13: Branching example (CanYouDrive.docm)

Notice how the program is structured (from youngest to oldest). Can you write a version that goes from oldest to youngest?

We will do Chapter 5 Exercises: 1,3,5,7,10,11

Chapter 4 test on February 16 (open book)

February 14: Checkboxes and Logical Operators


February 15:
Test Topics (test February 16):
  • number systems (you should know how to use the Excel spreadsheet to convert numbers to and from any of the following bases: binary, base 4, octal, decimal, base 16 - HEX)
  • You should know the basic structure of a VBA program (variables, input, calculation, output)
  • You should know how to properly name objects with appropriate prefixes (txt - text box, cmd - command button, lbl - label)
  • You should know how to use the 2 types of variables and name them properly (sgl - single for numbers, str - string for text)
  • You should know how to obtain input from a text box and assign variables a value
  • You should know how to do simple calculations using proper order of operations (PEDMAS)

February 16: Chapter 4 and Number Systems Test. Copy the Word DOCM file to your M: drive and write your answers in the word file and program the VBA in the word file. Submit your completed Word File to the Handin folder. Make sure the filename has your last name in it.

February 17: Global, Local and Local Static variables
GlobalLocalStatic.PNG
Each button was clicked 4 times. (see attached docm file for details)
  • The Global variable is reset only once (when the program is first run) so it counts the number of clicks correctly
  • The Local variable is reset every time you click on the local button
  • The static variable (also a local variable) is reset only once (when the program is first run) so it also counts the number of clicks correctly


February 27th: 5-10 Math Quiz (random numbers, global versus local variables)

March 2nd: 5-11 (radio buttons and check boxes, constants, global variables)


March 5-6: Some are ahead on Chapter 6 (do the even questions 6.2, 6-4, 6-6 etc)
6-2

For 6-4, you need to look at the Left, Mid and InStr functions (see the textbook)

Chapter 6 is largely about loops and string functions.

Here is a sample program that illustrates how the string functions work:

Here is some extra practice about Do Loops (WORKSHEET SOLUTIONS NOW ON H:\CS20\Chap6\Worksheets)

Here is some extra practice about FOR NEXT loops

Here is some extra practice on String Functions


March 19th: ASCII Code. If you recall from our work on binary numbers, all digital data is stored as binary (0's and 1's). In order to store text, the computer has to have a code which it uses to translate from text to binary and vice versa. Most (not all) computers use ASCII. Note that printable (eg. "a", "A") and non-printable (eg. space character = 32, return character = 13) have ASCII codes associated with them. Since capital letters come before small letters, this condition would evaluate as true:

If "Apple" < "apple" then
msgbox "Big Apple is less than small apple!"
end if

This is the same reason why "Allan" > "ALLAN"!

There are two Basic functions that convert between the two

CHR takes a number (ASCII code) and gives you the character: chr(65) = "A"

ASC takes a character and gives you the ASCII code for that character" asc("a") = 97



6-6

6-8
6-10 Count Vowels:

March 21: Here is an example file which shows you how to use an inputbox

March 22nd: (WORKSHEET SOLUTIONS NOW POSTED ON DROPBOX H:\CS20\Chap6\Worksheets)
6-14 Do this before 6-12
6-12 Just do the ENCODE part

March 30th: Test on Chapter 6 next Thursday April 5. If you are not going to be here on April 5, then write it on April 4th. There will be a closed book written section based on the three worksheets (FOR NEXT, DO LOOP, STRINGS) worth 33% of the test. The other 67% of the test will be for a computer program in VBA.

April 16: Click here for functions. Those who did not write the test before break will write the test on the next block day (April 26). Part B of your test will be different from that written by the other students.

April 20: Subroutines and passing parameters by reference. Quadratic.docm illustrates these concepts
quadratic.JPG
REDO 7-7, but use SUBS instead of Functions.

metric.JPG
To work with ComboBoxes, look at the attached DOCM:
(notice you have to fill the comboBox items in the UserForm Initialize event)

April 23rd (St George's Day):
  • Those working ahead, please start arrays for now (chapter 9). Click here.
  • The rest of you should finish up 7-7 (subs and function versions) and do 7-9.
  • students who did not write the chapter 6 test have a LAST CHANCE to write it this upcoming block Thursday.

April 24: We did 8-1 together and you need to finish up 8-9


8-1 also introduces the idea of a boolean function and boolean variable. A boolean type can take a value of TRUE or a value of FALSE. In the following example we use the math functions sqr() to find the square root of a number and int() to round a number down.

private function IsPerfectSquare(byval number as single) as Boolean
If sqr(number) = int(sqr(number)) then
IsPerfectSquare = true
Else
IsPerfectSquare = false
End if
end function

can also be written as

private function IsPerfectSquare(byval number as single) as Boolean
IsPerfectSquare = sqr(number) = int(sqr(number))
end function

The confusion in the short version of the function is that the operator = is used in two ways. The first = is used to assign the True or False value to the function IsPerfectSquare. The second = is used as a conditional operator:
if sqr(number) = int(sqr(number))
we don't need to put the if in as we're checking if the condition
sqr(number) = int(sqr(number))
is true or false.

April 25: 8-6 This will demonstrate how to use listboxes. There are hints as to how to solve this one. You will also need the results of 8-1 to solve 8-6

  • students who did not write the chapter 6 test have a LAST CHANCE to write it this upcoming block Thursday.

April 26: 8-11 Area of a triangle (don't forget to change the degrees to radians before using the sine function!)


April 30: Arrays (see files posted in Chapter 9 folder of the H: drive)
9-2, 9-3 and FamousNames example are contained in this ZIP file
Digitized Bus @ Holy Cross May 3rd (click here for map locatin of Holy Cross)
Bus transportation has been arranged for your student. PLEASE ASK YOUR STUDENT TO CATCH THE BUS AT HOLY CROSS-- PLEASE HAVE THEM MEET INSIDE THE FRONT DOORS OF HOLY CROSS NOT LATER THAN 8:35 AM. GARY VRINTEN WILL BE THE CONTACT AT HOLY CROSS. The bus will drop the students off at the Arts Building at the U of S.

The bus will pick the students up at approximately 2:30 pm from the U of S for drop off at approximately 2:45.

May 7: 9-7 Odd and Even numbers. Here is the template, finish it.

Test on Chapter 7 and 8 this Thusrday May 10. Open book, but no computers!

May 9th Review Answers
May 10th: Subroutines and Functions Test
May 11: Hangman Finish it. You can use this ZIP file of pictures if you want (it also has a list of 400 words which can be read into an array).
The images, word list and Word file are found in this ZIP file.

Put the Word file and the pictures in the same folder. Use the same technique we used in the Zodiac program:

mypath = ThisDocument.Path 'get the location of the word file
picturefile = mypath + "\pict0.jpg"
Image1.Picture = LoadPicture(picturefile)

Hangman.PNG
Your teacher's program


May 22: Modifying battleship. The original came from here.
Take the excel file and add some sounds to it. Take a look at this link for how-to. There are some sound files you can use on the H: drive. I got some audio files from this site. They are Creative Commons Attribution 3.0 license. This means you can use the audio clips for free, but you MUST say where you got the file from and who it belongs to.

You will need to add the code to play the WAV files into of the VBA Project. Put this right under the last Public declaration of Module 1. A module lets you use code in any part of the project (this project uses more than one form, so a module is needed.

module.JPG
Credits.PNG
This is what your teacher's looks like in action (click here for Youtube video)


May 23rd: Modify this tic-tac-toe with sound files. Make sure you use creative commons audio!

Block Thursday May 24: We will attempt to program a card game today and use actual images of cards. I obtained some Creative Commons images of a card deck here and converted them to JPG so we can use them in VBA. Remember, you should give credit for the card images in your program! I used Irfanview to convert the PNG graphics into JPG files which VBA can use in an image box. I've started it off for you. Work together in teams and finish it off. Save the ZIP file and unzip it to your network drive.

Work in groups of about 4. EACH student needs to record in a document (Word or Google doc etc) what your contribution was and what your group accomplished. The individual report as well as your group's folder with the game should be submitted for grading:
  • Individual Report 10
  • Group Game Result 10

2012 May 25: Write up your report and you can also make the game your own now. Add some touches like sound or try to improve the game on your own (from your group results yesterday - DON'T START FROM SCRATCH). Hand it in today!

2012 May 28: Periodic Table Project. This will probably be the last project in CS20. I haven't been able to find a replacement for control arrays (which are available in VB6 and VB.net). So, it will be a little more tedious, but it will let us review arrays! I've started the user interface out for you - finish it off and then we can start doing the coding.

Finally figured out how to do it with quasi-control arrays (used this very useful link). Pretty much everything needs to be done in the module though. USE THIS ONE


VBA-PT-Screenshot.PNG

2012 May 29: User defined types - Records
We will look at an example involving planetary data in a text file (OrbitalData.txt) and introduce the concept of records - a user defined type to store the information. Word file, PDF file with notes (handed out in class) and the textfile used by Records.docm are available below:


2012 May 30: User defined types and the periodic table project
Use this textfile to input the data into arrays so that you end up with a working periodic table.


PeriodicTableComplete.PNG
We will add search and sort next


Project Grading (65 marks):
  • Having all the buttons on the form: 10
  • Adding the legend (as shown in Page 2 of the word document) 10
  • Declaring the record type to store a single element 5 (this goes under the module Module 1)
  • Declaring the array to store all 118 elements 5 (this goes under the module Module 1)
  • Reading the data (name, symbol, atomic mass) from the text file into the array of records 10 (this goes under the UserForm_Initialize() event)
  • Adding search by atomic number 5 (this goes in the Userform - use a radio button and a user defined sub)
  • Adding search by element name 5 (this goes in the Userform - use a radio button and a user defined sub)
  • Adding search by element symbol 5 (this goes in the Userform - use a radio button and a user defined sub)
  • Adding sort by element name 5 (this goes in the Userform - use a radio button and a user defined sub)
  • Adding sort by element mass 5 (this goes in the Userform - use a radio button and a user defined sub)

PT-Project-Sections.PNG
2012 June 5: The searches should work out something like below. I added the legend by doing a screen capture of the original image and then inserting the captured image into a picture box. I also had to adjust my button colours for the transition metals and lanthanoids. The only thing needed now is sort (for full marks).

PT_Search.PNG

You will need to modify this example (I added a search by name to it). The code goes under the USERFORM, not in the module as I thought.


2012 June 6: Work on finishing the search options. We will introduce sorting on Friday. Note the grading scheme above with information on where each aspect of the program goes. Also, you cannot use command buttons for the sort and search because if you do, your command button will point to the button_click() event for the quasi-control array of buttons we defined in the class module (Class 1).

2012 June 7: Bubble sort is the simplest (and least efficient) type of sort. The notes are attached as well as a revision of the Planets Data Records Example which has sorting by name. This is also found on the network drive H:\CS20\Periodic Table VBA\Records Example\With Sort Search



You were also given a copy of this handout today:

Sorted.PNG
Records sorted by name


2012 June 8: The sort should be straightforward if you simply take the code for the planets example and modify it for your periodic table (copy and paste - don't retype). Your final product should looks something like:
PT_sortsearch.PNG

For a large project, it is wise to keep different versions as you develop your program. At each major stage, I created a new folder and copy and pasted the program to a new folder and developed the revised version in a new folder:

PT_versions.PNG